package cn.fyl.basic.RocketMq;

import cn.fyl.order.domain.OrderPetAcquisition;
import cn.fyl.order.mapper.OrderPetAcquisitionMapper;
import cn.fyl.order.service.IOrderPetAcquisitionService;
import cn.fyl.pet.mapper.PetMapper;
import cn.fyl.pet.mapper.SearchMasterMsgMapper;
import io.netty.util.CharsetUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * rocketmq 消息监听，@RocketMQMessageListener中的selectorExpression为tag，默认为*
 */
@Slf4j
@Component
@RocketMQMessageListener(topic = "queue_test_topic",selectorExpression="*",consumerGroup = "queue_group_test")
public class RocketMQMsgListener implements RocketMQListener<MessageExt> {

    @Autowired
    private IOrderPetAcquisitionService orderPetAcquisitionService;
    @Autowired
    private OrderPetAcquisitionMapper orderPetAcquisitionMapper;
    @Autowired
    private SearchMasterMsgMapper searchMasterMsgMapper;
    @Autowired
    private PetMapper petMapper;


    @Override
    public void onMessage(MessageExt message) {
        byte[] body = message.getBody();
        String msg = new String(body, CharsetUtil.UTF_8);
        OrderPetAcquisition order = orderPetAcquisitionService.loadBySn(msg);
        if(order.getState()==0){
            OrderPetAcquisition opa = orderPetAcquisitionMapper.loadBySn(msg);
            opa.setState(-1);
            orderPetAcquisitionMapper.update(opa);

            searchMasterMsgMapper.reject(opa.getShopId());

            petMapper.remove(opa.getPetId());
            System.out.println("订单已取消");
        }
    }

}
